package code._15network.udp.v4;

import untils.NetworkUtils;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SendTask implements Runnable{
    DatagramSocket datagramSocket;
    String IP;
    int port;

    public SendTask(DatagramSocket datagramSocket, String IP, int port) {
        this.datagramSocket = datagramSocket;
        this.IP = IP;
        this.port = port;
    }

    @Override
    public void run(){
        Scanner scanner = new Scanner(System.in);
        while (true){
            String msg = scanner.nextLine();
            try {
                DatagramPacket sendPacket = NetworkUtils.getSendPacket(msg, IP, port);
                datagramSocket.send(sendPacket);
            } catch (UnknownHostException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
